package com.gets.web.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gets.web.model.HotKeywordEntity;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by me675 on 2016/10/7.
 */
public class GetHotKeyword {
    public static void main(String args[]) {
        for (int i = 0; i < 17; i++) {
            try {
                get(i);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void get(int i) throws IOException {
        String url = "http://114.215.137.225/hotKeyword?p=" + i;
        String userAgent = "Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; MI 4LTE Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 XiaoMi/MiuiBrowser/2.1.1";
        String hotKeyword = Jsoup.connect(url).userAgent(userAgent)
                .timeout(12000).ignoreContentType(true).post().body().text();
        List<HotKeywordEntity> list = JSON.parseObject(hotKeyword, new TypeReference<ArrayList<HotKeywordEntity>>() {
        });
        for(HotKeywordEntity hotKeywordEntity:list){
            MysqlHelper mysqlHelper=new MysqlHelper();
            try {
                mysqlHelper.addDeleteModify("insert into hot_keyword values ("+hotKeywordEntity.getId()+","+hotKeywordEntity.getCount()+",'"+hotKeywordEntity.getName()+"')",null);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                mysqlHelper.closeconnection();
            }
        }
    }
}
